package com.java110.mall.cart.cmd.refundOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.storeOrder.StoreOrderCartReturnDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.cart.IStoreOrderCartReturnInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 门店查询 退款订单
 */
@Java110Cmd(serviceCode = "refundOrder.queryShopRefundOrders")
public class QueryShopRefundOrdersCmd extends Cmd {

    @Autowired
    private IStoreOrderCartReturnInnerServiceSMO storeOrderCartReturnInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateShop(context);
        Assert.hasKeyAndValue(reqJson,"shopId","未包含店铺编号");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        // 将link 转为personId
        String link = reqJson.getString("link");
        if (!StringUtil.isEmpty(link)) {
            UserDto userDto = new UserDto();
            userDto.setTel(link);
            List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
            if (ListUtil.isNull(userDtos)) {
                return;
            }
            reqJson.put("personId", userDtos.get(0).getUserId());
            reqJson.remove("link");
        }
        StoreOrderCartReturnDto storeOrderCartReturnDto = BeanConvertUtil.covertBean(reqJson, StoreOrderCartReturnDto.class);

        int count = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturnsCount(storeOrderCartReturnDto);

        List<StoreOrderCartReturnDto> storeOrderCartReturnDtos = null;
        if (count > 0) {
            storeOrderCartReturnDtos = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturns(storeOrderCartReturnDto);
        } else {
            storeOrderCartReturnDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartReturnDto.getRow()), count, storeOrderCartReturnDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);

    }
}
